@extends("layout.app")

@section("content")
<style>
    .form-group2 {
    	margin-top:20px;
    }
</style>
    <div class="form-inline">

        <div class="form-group has-success">
            <input class="form-control" id="begin_time" placeholder="开始时间"/>
        </div>
        <div class="form-group has-success">
            <input class="form-control" id="end_time" placeholder="结束时间"/>
        </div>
        <div class="form-group has-success">
            &nbsp;<button id="button" class="btn btn-default" onclick="jQuery('#rowed3').setGridParam({postData: postData()}).trigger('reloadGrid')"><i class="fa fa-search"></i> 查询</button>
        </div>
        <div class="form-group has-success">
            &nbsp;<button id="button" class="btn btn-success" onclick="setDate('<?php echo date("Y-m-d", strtotime("Monday this week"))?>', '')"> 本周充值</button>
        </div>
        <div class="form-group has-success">
            &nbsp;<button id="button" class="btn btn-info" onclick="setDate('<?php echo date("Y-m-d", strtotime("Monday this week")-604800) ?>', '<?php echo date("Y-m-d", strtotime("last Sunday"))?>')"> 上周充值</button>
        </div>
        <div class="form-group has-success">
            &nbsp;<button id="button" class="btn btn-warning" onclick="setDate('<?php echo date("Y-m-01")?>', '')"> 本月充值</button>
        </div>
        <div class="form-group has-success">
            &nbsp;<button id="button" class="btn btn-danger" onclick="setDate('<?php echo date("Y-m-01",  strtotime(date("Y-m-01") . " -1 month"))?>', '<?php echo date("Y-m-d",  strtotime(date("Y-m-01") . " -1 day"))?>')"> 上月充值</button>
        </div>
    </div>
    <div class="separator"></div>
    <div class="box-body">

        <table id="rowed3"></table>
        <div id="prowed3"></div>

    </div>
<script>

    $(document).ready(function() {

        var data = {
            url :"/agent/chao",
            postData: postData(),
            colNames: ["序号", "账号", "姓名", "微信","手机号", "银行卡号", "开户银行", "伙伴人数","玩家人数", "收益余额","保险箱", "收益明细", "比例设置(%)", "备注", "操作"],
            colModel: [
                {
                    name: "id",
                    key: true
                }, {
                    name: "email",
                    sortable: false,
                    editable: true
                }, {
                    name: "name",
                    sortable: false,
                    editable: true
                }, {
                    name: "weixin",
                    sortable: false,
                    editable: true
                }, {
                    name: "phone",
                    sortable: false,
                    editable: true
                }, {
                    name: "bankcard",
                    sortable: false,
                    editable: true
                }, {
					name: "bank_name",
					sortable: false,
                    editable: true
                }, {
                    name: "c1",
                    sortable: false,
                    search: false,
                    formatter: function(cellvalue, options, object) {
                        return '<a href="/agent/zongdai?chao_zongdai='+object.id+'"><span class="text-success">'+cellvalue+'</span></a>';
                    }
                }, {
                    name: "c2",
                    sortable: false,
                    search: false,
                    formatter: function(cellvalue, options, object) {
                        return '<a href="/user?chao_zongdai='+object.id+'"><span class="text-success">'+cellvalue+'</span></a>';
                    }
                }, {
                    name: "money",
                    sortable: false,
                    search: false,
                    formatter: function(cellvalue, options, object) {
                        @if ($chaoz)
                        // return '<a href="javascript:;" onclick="addMoney2(1,\''+object.money2+'\')" data-toggle="modal" data-target="#addModal2"><span style="font-weight:bold;">'+cellvalue+'</span></a>';
                            $('#bxx1').html('<a href="javascript:;" onclick="addMoney2(1,\''+object.money2+'\')" data-toggle="modal" data-target="#addModal2"><span>转至余额</span></a>');
                            $('#bxx2').html('<a href="javascript:;" onclick="addMoney2(2,\''+object.money+'\')" data-toggle="modal" data-target="#addModal2"><span>转入保险箱</span></a>');
                        return cellvalue
                        @else
                        return '<a href="javascript:;" onclick="addMoney('+object.id+',\''+object.name+'\')" data-toggle="modal" data-target="#addModal"><span style="font-weight:bold;">'+cellvalue+'</span></a>';
                        @endif
                    }
                }, {
                    name: "money2",
                    sortable: false,
                    search: false,
                    formatter: function(cellvalue, options, object) {
                        @if ($chaoz)
                            return '<a href="javascript:;" onclick="addMoney2(2,\''+object.money+'\')" data-toggle="modal" data-target="#addModal2"><span style="font-weight:bold;">'+cellvalue+'</span></a>';
                        @else
                            return cellvalue;
                        @endif
                    }
                }, {
                    name: "id",
                    sortable: false,
                    search: false,
                    formatter: function(cellvalue, options, object) {
                        return '<a href="/agent/chao/detail?chao='+object["id"]+'"><i class="fa fa-link text-success"></i></a>';
                    }
                }, {
                    name: "ratio",
                    <?php if (!$chaoz):?>
                    editable: true,
                    editrules: {
                        minValue: 0,
                        maxValue: 100
                    }
                    <?php endif ?>
                }, {
                    name: "desc",
                    sortable: false,
                    search: false,
                    editable: true

                }, {
                    name: "id",
                    sortable: false,
                    search: false,
                    formatter: function(cellvalue, options, object) {
                    	<?php if ($chaoz):?>
                    		return '<a href="javascript:;" onclick=" return verify('+object.id+')"><span class="text-success">申请提款</span></a>';
                    	<?php else:?>
                    		return '<a href="javascript:;" onclick="resetpwd('+object.id+')" data-toggle="modal" data-target="#pwModal"><i class="fa fa-key text-danger" style="width:18px;"></i></a>';
                    	<?php endif;?>
                        }
                }],
            caption: '<i class="fa fa-list"></i> {{$label2}}',
            sortname: "money",
            sortorder: "desc",
            editurl: "/agent/chao/edit",
            gridComplete: function() {
                $(this).footerData("set", {
                    "c1"      : '<?php echo $c1?>',
                    "c2"      : '<?php echo $c2?>',
                });
            }
        };

        var add = true;
        var del = true;
        var edit = true;
        <?php if ($chaoz):?>
        add = false;
        del = false;
        edit = false;
        <?php endif;?>

        jqGrid(data, edit, add, del);

        $(".form-inline .form-control").datepicker({
            dateFormat: 'yy-mm-dd',
            monthNames: ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
            dayNamesMin: ['日','一','二','三','四','五','六'] });

    });

    var loader = "<div style='text-align:center'><img src='/img/loaders/15.gif'></div>";

    function setDate(begin_time, end_time) {

        $("#begin_time").val(begin_time);
        $("#end_time").val(end_time);

        jQuery('#rowed3').setGridParam({postData: postData()}).trigger('reloadGrid')
    }

    function postData() {

        return {
            begin_time: $("#begin_time").val(),
            end_time: $("#end_time").val()
        }
    }
    $(document).ready(function weixin(){
        <?php if (!$request->session()->get("openid")):?>
        $("#shenqing").toggle();
        $("#weixin").toggle();
        <?php endif;?>
    });

    function resetpwd(id) {
    	$("#resetpwd-id").next().remove();
    	$("#pw-button").attr("disabled",false);
    	$("#pw-cancel").attr("disabled",false);
    	$("#resetPwdSpan").html("确定重置[" + id + "]的密码?");
    	$("#pwModal").modal([]);
    	$("#resetpwd-id").val(id);
    }

    function updatepw(id) {
    	$("#pw-button").attr("disabled","disabled");
    	$("#pw-cancel").attr("disabled","disabled");
    	$("#resetpwd-id").after(loader);
    	$.post("/agent/chao/resetpwd",{id: id}, function(result) {
    		if (result.success) {
    			$("#rowed3").trigger("reloadGrid");
    	        $.gritter.add({
    	            text:'重置密码成功',
    	            sticky: false,
    	            time: '',
    	            class_name: 'gritter-light'});

    		} else {
    			 $.gritter.add({
    		        title: '错误信息',
    		        text:'操作失败',
    		        sticky: false,
    		        time: ''});
    		}
    		$("#pwModal").modal('hide');
    	});
    }

    function verify(id){

        <?php if($chaoz && $chaoz->weixin):?>
        $('#fhModal').modal({});
        $('#chao_id').val(id);
        return false;
        <?php else:?>
        alert("请联系客服完善个人信息");
        return true;
        <?php endif;?>
    }

    function onSubmit() {

        $("#chao_id").val()
        <?php if ($chaoz && !$chaoz->pwd2):?>
        if($("input[name=pwd2]").val() != $("input[name=password2]").val()) {
            $("#notice").show();
            return false;
        }
        <?php endif?>

        var rmb = $("input[name=rmb]").val();
        if (parseInt(rmb).toString() != rmb) {
            alert("请填写整数金额");
            return false;
        }

        if (parseInt(rmb) < 1000) {
            alert("提款金额不可低于1000元");
            return false;
        }

        if (parseInt(rmb) % 1000 != 0) {
            alert("请输入1000的整数倍");
            return false;
        }

        if (parseInt(rmb) > <?php echo $money?>) {
            return false;
        }
        return true;
    }

    function check(id) {
        $("#checkSpan").append("请设置合伙人" + id + " 的总分成比例");
        $("input[name=id]").val(id);
    }

    function transfer(id, money) {
        if(money < 0 || money > 99999999){
            $("#notice1").show();

        }else{
            $("#addModal").modal('hide');
            $("#notice1").hide();
            $.post("/chao/money/add",{id: id, money: money}, function(result){
                if (result.success) {
                    $("#rowed3").trigger("reloadGrid");
                    $.gritter.add({
                        text:'转账成功',
                        sticky: false,
                        time: '',
                        class_name: 'gritter-light'});

                } else {
                    $.gritter.add({
                        title: '错误信息',
                        text:'操作失败',
                        sticky: false,
                        time: ''});
                }
            }).error(function(err){
                $.gritter.add({
                    title:  '错误信息',
                    text:   err,
                    sticky: false,
                    time:   ''});
            });
        }
    }
    function transfer2(id, money) {
        if(money < 0 || money > 99999999){
            $("#notice1").show();

        }else{
            $("#addModal2").modal('hide');
            $("#notice12").hide();
            $.post("/chao/money/add2",{id: id, money: money}, function(result){
                if (result.success) {
                    $("#rowed3").trigger("reloadGrid");
                    $.gritter.add({
                        text:result.message,
                        sticky: false,
                        time: '',
                        class_name: 'gritter-light'});

                } else {
                    $.gritter.add({
                        title: '错误信息',
                        text:result.message,
                        sticky: false,
                        time: ''});
                }
            }).error(function(err){
                $.gritter.add({
                    title:  '错误信息',
                    text:   err,
                    sticky: false,
                    time:   ''});
            });
        }
    }

    function addMoney(id, name) {
        $("#notice").hide();
        $("#addModal").modal([]);
        $("#addModalLabel").html("向[<span class='text-danger'>" + name + "</span>]转账");
        $("#money_id").val(id);
        $("#add_money").val('');
    }
    function addMoney2(id,num) {
        $("#notice2").hide();
        $("#addModal2").modal([]);
        if(id == 1) {
            $("#addModalLabel2").html("保险箱转入余额(保险箱:<span class='text-danger'>" + num + "</span>元)");
        } else{
            $("#addModalLabel2").html("余额转入保险箱(余额:<span class='text-danger'>" + num + "</span>元)");
        }
        $("#money_id2").val(id);
        $("#add_money2").val('');
    }
    <?php if($request->session()->get("message")):?>
    alert("<?php echo $request->session()->get("message")?>");
    <?php endif?>

</script>

<div class="modal fade" id="pwModal" tabindex="-1" role="dialog" data-backdrop="static" aria-labelledby="pwModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-body">
                <h4 class="modal-title"><span id="resetPwdSpan" class="text-danger"><i class="fa fa-key"></i></span></h4>
                <input type="hidden" id="resetpwd-id"/>
            </div>
            <div class="modal-footer">
                <button id="pw-button" type="submit" class="btn btn-success" onclick="updatepw($('#resetpwd-id').val())"><i class="fa fa-check"></i> 确定</button>
            	<button id="pw-cancel" type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="fhModal" tabindex="-1" role="dialog" aria-labelledby="fhModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
			<div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-name="Close"><span aria-hidden="true">×</span></button>
                <h4 class="modal-title">申请提款(余额:<?php echo $money ?>)</h4>
            </div>
            <div class="modal-body">
            <form id="qvkuan" method="post" action="/chao/tikuan" onsubmit="return onSubmit();">
                {!! csrf_field() !!}
                <input type="hidden" name="chao_id" id="chao_id" />
                <label class="akh aki apr"></label>
                <label>提款金额(请输入1000的整数倍)</label>
                <input type="number" class="form-control" placeholder="提款金额(请输入1000的整数倍)" name="rmb" value="" /><br/>
            </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-success" onclick="$('#qvkuan').submit(); return false;"> 提 交</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="addModal" tabindex="-1" role="dialog" data-backdrop="static" aria-labelledby="addModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-name="Close"><span aria-hidden="true">×</span></button>
                <h4 class="modal-title" id="addModalLabel"></h4>
            </div>
            <div class="modal-body">
                <input type="hidden" id="money_id" />
                <div class="form-group">
                    <label> 转账数量 </label>
                    <input id="add_money" class="form-control" type="number" />
                </div>
                <label id="notice1" class="text-danger" style="display:none;">转账数量有误,请重新输入</label>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-success" onclick="transfer($('#money_id').val(),$('#add_money').val())"><i class="fa fa-lock"> 确定</i></button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="addModal2" tabindex="-1" role="dialog" data-backdrop="static" aria-labelledby="addModalLabel2">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-name="Close"><span aria-hidden="true">×</span></button>
                <h4 class="modal-title" id="addModalLabel2"></h4>
            </div>
            <div class="modal-body">
            	<input type="hidden" id="money_id2" />
                <ul class="nav nav-tabs" role="tablist">
                    <li role="presentation" class="active"><a href="#bxx2" id='bxx2' aria-controls="bxx2" role="tab" data-toggle="tab"></a></li>
                    <li role="presentation"><a href="#bxx1" id='bxx1' aria-controls="bxx1" role="tab" data-toggle="tab"></a></li>
                </ul>
                <div class="form-group form-group2">
                    <label> 转账数量 </label>
                    <input id="add_money2" class="form-control" type="number" />
                </div>
                <label id="notice2" class="text-danger" style="display:none;">转账数量有误,请重新输入</label>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-success" onclick="transfer2($('#money_id2').val(),$('#add_money2').val())"><i class="fa fa-lock"> 确定</i></button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

@endsection